package com.library.web.sys_book.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.annotation.Auth;
import com.library.utils.ResultUtils;
import com.library.utils.ResultVo;
import com.library.web.sys_book.pojo.ListParm;
import com.library.web.sys_book.pojo.SysBooks;
import com.library.web.sys_book.pojo.ExportVo;
import com.library.web.sys_book.service.SysBooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/api/books")
public class SysBooksController {
    @Autowired
    private SysBooksService sysBooksService;

    //新增
    @Auth
    @PostMapping
    public ResultVo add(@RequestBody SysBooks books) {
        boolean save = sysBooksService.save(books);
        if (save) {
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    //编辑
    @Auth
    @PutMapping
    public ResultVo edit(@RequestBody SysBooks books) {
        boolean save = sysBooksService.updateById(books);
        if (save) {
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    //删除
    @Auth
    @DeleteMapping("/{bookId}")
    public ResultVo delete(@PathVariable("bookId") Long bookId) {
        boolean remove = sysBooksService.removeById(bookId);
        if (remove) {
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    //列表
    @Auth
    @GetMapping("/list")
    public ResultVo getList(ListParm parm) {
        IPage<SysBooks> list = sysBooksService.getList(parm);
        return ResultUtils.success("查询成功", list);
    }

    //导出excel
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        ExportVo exportVo = new ExportVo();
        exportVo.setExportRemark("导入时的图书分类仅有以下10种,请在以下10种分类中选择相应的图书分类:1.马列主义2.计算机科学技术3.史学理论4.测绘学5.林业6.环境科学理论7.管理学8.机械工程9.中国军事10.绘画");
        //从数据库查询出所有的数据
        List<ExportVo> list = sysBooksService.exportBook();
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("bookName", "图书名称");
        writer.addHeaderAlias("categoryName", "图书分类");
        writer.addHeaderAlias("bookCode", "ISBN号");
        writer.addHeaderAlias("bookAuther", "图书作者");
        writer.addHeaderAlias("bookProduct", "出版社");
        writer.addHeaderAlias("bookPlaceNum", "书架号");
        writer.addHeaderAlias("bookPrice", "图书价格");
        writer.addHeaderAlias("bookStore", "图书库存");
        writer.addHeaderAlias("exportRemark","分类备注,该列可以删除,仅当提示信息,不影响任何操作!");
        list.add(exportVo);
        //一次性写出list内的对象到excel 使用默认样式,强制输出标题
        writer.write(list, true);
        //设置浏览器的响应格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("图书信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
    }

    //导入excel
    @PostMapping("/import")
    public ResultVo importBooks(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<SysBooks> importVos = reader.readAll(SysBooks.class);
        boolean b = false;
        for (SysBooks sysBooks : importVos) {
            switch (sysBooks.getCategoryName()) {
                case "马列主义":
                    sysBooks.setCategoryId(1L);
                    break;
                case "计算机科学技术":
                    sysBooks.setCategoryId(2L);
                    break;
                case "史学理论":
                    sysBooks.setCategoryId(3L);
                    break;
                case "测绘学":
                    sysBooks.setCategoryId(4L);
                    break;
                case "林业":
                    sysBooks.setCategoryId(5L);
                    break;
                case "环境科学理论":
                    sysBooks.setCategoryId(6L);
                    break;
                case "管理学":
                    sysBooks.setCategoryId(7L);
                    break;
                case "机械工程":
                    sysBooks.setCategoryId(8L);
                    break;
                case "中国军事":
                    sysBooks.setCategoryId(9L);
                    break;
                case "绘画":
                    sysBooks.setCategoryId(10L);
                    break;
                default:
                    return ResultUtils.error("导入失败!");
            }
            b = sysBooksService.save(sysBooks);
        }
        if (b) {
            return ResultUtils.success("导入成功!");
        } else {
            return ResultUtils.success("导入失败!");
        }
    }
}
